<template>
	<el-dialog title="添加服务调用" :visible.sync="dialogVisible" width="600px" @close="handleClose"
		:close-on-click-modal="false">
		<div slot="title" class="dialog-header-title">
			<i class="el-icon-edit-outline" ></i>
			<span>添加服务调用</span>
		</div>

		<div class="smp-search">
			<el-form :inline="false" label-width="100px" size="mini" ref="form" :model="form" :rules="rules">

				<el-row>
					<el-col :span="24">
						<el-col :span="24">
							<el-form-item label="服务编码:">
								<el-input v-model="form.pubCode" placeholder="服务编码" :disabled="true"></el-input>
							</el-form-item>
						</el-col>

						<el-col :span="24">
							<el-form-item label="系统编号:" prop="pubSysCode">
								<el-input v-model="form.pubSysCode" placeholder="系统编号" @blur="inputBlur"></el-input>
							</el-form-item>
						</el-col>

						<el-col :span="24">
							<el-form-item label="报文编码:" prop="msgCode">
								<el-select v-model="form.msgCode" style="width: 100%" placeholder="报文编码">
									<el-option v-for="item in dict('SMP0004')" :key="item.itemCode"
										:label="item.itemName" :value="item.itemCode">
									</el-option>
								</el-select>
							</el-form-item>
						</el-col>

						<el-col :span="24">
							<el-form-item label="报文协议:" prop="msgAgreement">
								<el-input v-model="form.msgAgreement" placeholder="报文协议"></el-input>
							</el-form-item>
						</el-col>
						
						<el-col :span="24">
							<el-form-item label="代理路径:" prop="proxyPath">
								<el-input v-model="form.proxyPath" placeholder="代理路径"></el-input>
							</el-form-item>
						</el-col>
						
						<el-col :span="24">
							<el-form-item label="关联需求:" prop="associatedRequirement">
								<el-input v-model="form.associatedRequirement" placeholder="关联需求"></el-input>
							</el-form-item>
						</el-col>
					</el-col>
				</el-row>


			</el-form>
		</div>
		<span slot="footer" class="dialog-footer">
			<el-button size="mini" type="primary" @click="onSubmit">提交</el-button>
			<el-button size="mini" @click="reset">清空</el-button>
			<el-button size="mini" @click="closeDialog">取消</el-button>
		</span>
	</el-dialog>
</template>

<script>
	"use strict";
	import {
		addServiceInfo,getSystemInfo
	} from '@/api/service.js';

	export default {

		data() {
			return {
				dialogVisible: false,
				pubCode:'',
				rules: {
					pubCode: [{
						required: true,
						message: "服务编码不能为空",
						trigger: ['blur', 'change']
					}],
					pubSysCode: [{
						required: true,
						message: "消费系统编号不能为空",
						trigger: ['blur', 'change']
					}],
					msgCode: [{
						required: true,
						message: "报文编码不能为空",
						trigger: ['blur', 'change']
					}],
					msgAgreement: [{
						required: true,
						message: "报文协议不能为空",
						trigger: ['blur', 'change']
					}],
					proxyPath: [{
						required: true,
						message: "代理路径不能为空",
						trigger: ['blur', 'change']
					}],
					associatedRequirement: [{
						required: true,
						message: "关联需求不能为空",
						trigger: ['blur', 'change']
					}],
				},
				form: {
					pubCode: '',
					pubSysCode: '',
					msgCode: '',
					msgAgreement: '',
					proxyPath: '',
					associatedRequirement:''
				},

			};
		},



		methods: {

			onSubmit() {
				this.serviceRegister();
			},
			
			inputBlur(){
				getSystemInfo({
					"systemNo": this.form.pubSysCode
				}).then(response => {
					this.form.msgCode = response.data.extend.msgCode;
					this.form.msgAgreement = response.data.extend.msgAgreement;
				}).catch(error => {
					console.info("失败");
					console.info(error.message);
				});
			},
			
			reset() {
				this.pubCode = this.form.pubCode;
				this.form = {};
				this.form.pubCode = this.pubCode;
			},

			serviceRegister() {
				this.$refs["form"].validate(valid => {
					if (valid) {
						addServiceInfo(this.form).then(response => {
							this.data = response.data.extend;
							this.$message({
						duration:5000,
								type: 'success',
								message: '添加服务调用成功！'
							});
							this.form = {};
							this.$refs["form"].resetFields();
							this.dialogVisible = false;
							this.$emit("getInit");
						}).catch(error => {
							this.$message({
						duration:5000,								type: 'error',
								message: '添加服务调用失败！'
							});
							this.form = {};
							this.$refs["form"].resetFields();
							this.dialogVisible = false;
							/* this.$emit("getInit"); */
						});
					}
				})

			},

			showDialog(pubCode) {
				this.form.pubCode = pubCode;
				this.dialogVisible = true;
			},

			closeDialog() {
				this.$refs["form"].resetFields();
				this.dialogVisible = false;
			},

			handleClose() {
				this.$refs["form"].resetFields();
				this.dialogVisible = false;
			}


		},

	}
</script>

<style>
</style>
